/*******************************************************************************
 * Copyright (c) 2011 Olexandr V. Shaposhnikov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Olexandr V. Shaposhnikov - initial API and implementation and/or initial documentation
 *******************************************************************************/
package com.funkyroach.use.ui.wizard;

import java.io.File;
import java.net.URI;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.dltk.core.environment.EnvironmentManager;
import org.eclipse.dltk.core.environment.IEnvironment;
import org.eclipse.dltk.launching.IInterpreterInstall;
import org.eclipse.dltk.ui.wizards.ILocationGroup;
import org.eclipse.dltk.ui.wizards.ProjectWizardFirstPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.funkyroach.use.core.language.USNature;
import com.funkyroach.use.udk.USUDKInstallType;

public class USNewProjectWizardFirstPage extends WizardPage implements ILocationGroup {

    private Text projectNameText;
    private Text udkHome;
    private boolean udkHomeValid;
    private String projectName;
    private String udkHomePath;

    /**
     * Create the wizard.
     */
    public USNewProjectWizardFirstPage() {
        super(ProjectWizardFirstPage.PAGE_NAME);
        setTitle("Create a New UnrealScript Project");
        setDescription("Select project name");
    }

    /**
     * Create contents of the wizard.
     * 
     * @param parent
     */
    public void createControl(Composite parent) {
        Composite container = new Composite(parent, SWT.NULL);
        setControl(container);
        container.setLayout(new GridLayout(2, false));

        Label lblProjectName = new Label(container, SWT.NONE);
        lblProjectName.setText("Project Name");

        projectNameText = new Text(container, SWT.BORDER);
        projectNameText.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                projectName = projectNameText.getText();
                checkPageComplete();
            }
        });
        GridData gd_text = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gd_text.widthHint = 389;
        projectNameText.setLayoutData(gd_text);
        new Label(container, SWT.NONE);
        new Label(container, SWT.NONE);
        new Label(container, SWT.NONE);
        new Label(container, SWT.NONE);

        Group grpUdk = new Group(container, SWT.NONE);
        GridData gd_grpUdk = new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1);
        gd_grpUdk.heightHint = 66;
        gd_grpUdk.widthHint = 459;
        grpUdk.setLayoutData(gd_grpUdk);
        grpUdk.setText("Select UDK Home (like C:\\UDK\\UDK-2011-09) Project will be create in this folder");
        grpUdk.setLayout(new GridLayout(4, false));

        Label lblUdkHome = new Label(grpUdk, SWT.NONE);
        lblUdkHome.setText("UDK Home");

        udkHome = new Text(grpUdk, SWT.BORDER);
        GridData gd_text_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gd_text_1.widthHint = 251;
        udkHome.setLayoutData(gd_text_1);
        udkHome.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                String file = udkHome.getText();
                if (file != null) {
                    udkHomePath = udkHome.getText();
                    File udkHomeDir = new File(udkHomePath);
                    if (udkHomeDir.isDirectory()){// && USUDKInstallType.retreiveVersion(udkHome.getText()) > 0) {
                        udkHomeValid = true;
                    } else {
                        udkHomeValid = false;
                    }
                    checkPageComplete();
                }
            }
        });
        new Label(grpUdk, SWT.NONE);

        Button btnNewButton = new Button(grpUdk, SWT.NONE);
        btnNewButton.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseDown(MouseEvent e) {
                DirectoryDialog d = new DirectoryDialog(getShell());
                d.setText(udkHome.getText());
                setUdkHome(d.open());
            }

        });
        btnNewButton.setText("Browse");

        Composite composite = new Composite(grpUdk, SWT.NONE);
        composite.setLayout(new FillLayout(SWT.HORIZONTAL));
        new Label(grpUdk, SWT.NONE);
        new Label(grpUdk, SWT.NONE);
        new Label(grpUdk, SWT.NONE);
        udkHomeValid = false;
        checkPageComplete();
    }

    private void setUdkHome(String path) {
        this.udkHome.setText(path);
    }

    private void checkPageComplete() {
        String projectName = projectNameText.getText();

        if (projectName != null && projectName.length() > 0 && udkHomeValid) {
            setPageComplete(true);
        } else {
            setPageComplete(false);
        }

    }

    @Override
    public String getProjectName() {
        return udkHome.getText();
    }

    @Override
    public IProject getProjectHandle() {
        return ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
    }

    @Override
    public IEnvironment getEnvironment() {
        return EnvironmentManager.getLocalEnvironment();
    }

    @Override
    public IPath getLocation() {
        return new Path(udkHome.getText());
    }

    @Override
    public URI getLocationURI() {
        return new File(getUDKHome()).toURI();
    }

    @Override
    public IInterpreterInstall getInterpreter() {
        return null;
    }

    @Override
    public boolean getDetect() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isExistingLocation() {
        return true;
    }

    @Override
    public boolean isInWorkspace() {
        return false;
    }

    @Override
    public boolean isSrc() {
        return false;
    }

    @Override
    public String getScriptNature() {
        return USNature.NATURE_ID;
    }

    public String getUDKHome() {
        return udkHomePath;
    }

}
